﻿using System.Collections;
using NetWork;
using UnityEngine;
using AssetLoad;

namespace Room
{
    public class RoomRaid : RoomBase
    {
        public override SceneType GetSceneType()
        {
            return SceneType.RAID;
        }

        public override ServerType GetServerType()
        {
            return ServerType.raid;
        }

        public static string fightstateparticlepath = "particles/03_buff/Buff_Ranshao";
        public static string rewardstateparticlepath = "particles/03_buff/Buff_Xuruo_buffb";
        public static string defeatstateparticlepath = "particles/03_buff/Buff_Mabi";


        bool isInited = false;
        public override void Init()
        {
            base.Init();

            netManager = new RoomNetRaid();
            netManager.Init(this);

            isInited = true;
        }

        protected override void OnEnter(params object[] param)
        {
            fightstateparticlepath ="particles/"+ Dict.Blo.DictParticleBlo.GetCommonParticle("Buff_Ranshao").ResourcePath;
            rewardstateparticlepath = "particles/" + Dict.Blo.DictParticleBlo.GetCommonParticle("Buff_Xuruo_buffb").ResourcePath;
            defeatstateparticlepath = "particles/" + Dict.Blo.DictParticleBlo.GetCommonParticle("Buff_Mabi").ResourcePath;
            base.OnEnter(param);
            if (!isInited) {
                Init();
            }
            ManagerController.Instance.cameraTool.SwitchSceneCamera();
            StartCoroutine(LoadResource());
            Foreground();
        }

        protected IEnumerator LoadResource() {
            yield return StartCoroutine(AssetBundleManager.Instance.LoadSceneAsync(SceneName.sceneFolder
                +"map_forestfield_001", "map_forestfield_001"));

            yield return StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadAssetBundleCoroutine(fightstateparticlepath));
            GameObjectPoolManager.Instance.Regist(fightstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(fightstateparticlepath));
            yield return StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadAssetBundleCoroutine(rewardstateparticlepath));
            GameObjectPoolManager.Instance.Regist(rewardstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(rewardstateparticlepath));
            yield return StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadAssetBundleCoroutine(defeatstateparticlepath));
            GameObjectPoolManager.Instance.Regist(defeatstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(defeatstateparticlepath));


            OnLoadComplete();
        }

        protected void OnLoadComplete() {
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle,0,0), cameraFOV);
            StaticData.cameraRotate = Vector3.zero;
            netManager.Enter();
            LoadingPanel.Instance.HideLoading();
            EasyTouchTool.Instance.ShowJoystick();
        }

        protected void UnloadResource() {
            GameObjectPoolManager.Instance.UnRegist(fightstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(fightstateparticlepath));
            AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(fightstateparticlepath);
            GameObjectPoolManager.Instance.UnRegist(rewardstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(rewardstateparticlepath));
            AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(rewardstateparticlepath);
            GameObjectPoolManager.Instance.UnRegist(defeatstateparticlepath, CommonMethod.GetAssetNameFromAssetBundleName(defeatstateparticlepath));
            AssetLoad.AssetBundleManager.Instance.LoadAssetBundle(defeatstateparticlepath);

        }

        protected override void OnLeave()
        {
            base.OnLeave();
            charactorManager.ClearAll();
            netManager.Leave();
            UnloadResource();
        }

        void OnCharactorCreate(RoomCharactor rc) {

        }

        void OnCharactorDestory(RoomCharactor rc) {

        }

        public override void OnMainCharactorLoaded(Transform mc)
        {
            base.OnMainCharactorLoaded(mc);
            viewController.SetPlayer(mc);
        }

        protected override void Tick()
        {
            base.Tick();
            netManager.Tick();
            viewController.Tick();
            charactorManager.Tick();
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
        }

        protected override void OnForeground()
        {
            netManager.Resume();
            viewController.Resume();
            uiManager.gameObject.SetActive(true);
            EasyTouchTool.Instance.ShowJoystick();
        }

        protected override void OnBackground()
        {
            netManager.Pause();
            viewController.Pause();
            uiManager.gameObject.SetActive(false);
            EasyTouchTool.Instance.Hide();
        }




    }
}
